// userPageC/pages/abulkList/abulkList.js
const app = getApp()
const WXAPI = require('../../../api/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule: 1,
      // 是否显示左上角图标   1表示显示    0表示不显示
      title: '门店团购',
      // 导航栏 中间的标题
      textColor: '#222',
      // 标题颜色
      bgColor: '#fff',
      // 导航栏背景颜色
      isloading: true,
      isColor: true 
    },
    navList:[
      {id: '', name: '全部'},
    ],
    shopImgList:[],
    current: '', 
    imgHttp: app.globalData.imgHttp,
    bulkList: [],
    isPull: true,//是否刷新
    lodingtype: 1,//加载状态（0：加载中  1：加载完成没有更多）
    pageSize: 10,
    pageNum: 1,
    shopTypeParent: '',
    lnt: '',
    lat: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const current = options.type
    this.setData({
      current: current,
      shopTypeParent: current,
    })
    this.getShopType()
    this.getBulkList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.login_dialog = this.selectComponent("#login_dialog");
    console.log('this.login_dialog',this.login_dialog)
    var token = wx.getStorageSync('token')
  },
  //获取类型
  getShopType(){
    WXAPI.shopType('',res => {
      if(res.code == 200){
        var navList = this.data.navList
        this.setData({
          navList: navList.concat(res.result)
        })
      }
    })
  },
  //获取团购列表
  getBulkList(){
    const that = this;
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    var data = {
      shopTypeParent: this.data.shopTypeParent,
      lnt: this.data.lnt,
      lat: this.data.lat,
      pageSize: this.data.pageSize,
      pageNum: this.data.pageNum,
    }
    WXAPI.bulkList(data, res => {
      const {size,total,records} = res.result;
      var list = [];
      var bulkList = this.data.bulkList;
      if (res.code === 200) {
        if (this.data.pageNum >= Math.ceil(total / size)) {
          this.setData({
            lodingtype: 1,
            isPull: false
          });
        } else {
          this.setData({
            lodingtype: 0,
            isPull: true
          });
        }

        wx.hideLoading();
        list = records, this.setData({
          bulkList: bulkList.concat(list)
        });
      }
    });
  },
  //横向滚动
  scroll(e){

  },
  //切换选项
  handleType(e){
    const id = e.currentTarget.dataset.id;
    this.setData({
      current: id,
      shopTypeParent: id,
      bulkList: [],
      pageNum: 1,
    })
    console.log('id',id)
    this.getBulkList();
  },
  //查看团购
  handleDetail(e){
    var id = e.currentTarget.dataset.id
    console.log('id',id)
    wx.navigateTo({
      url: '/userPageC/pages/abulkDetail/abulkDetail?id='+id,
     
    });
  },
  
  //立即购买
  handlepay(e){
    var id = e.currentTarget.dataset.id
    console.log('id',id)
    this.setData({
      grouponId: id
    })
    const token = wx.getStorageSync('token');
    if(token){
      wx.navigateTo({
        url: '/userPageC/pages/confirmOrder/confirmOrder?grouponId='+id,
      });
    }else{
      this.login_dialog.loginShow()
    }  
  },
  loginSuccess(){
    wx.navigateTo({
      url: '/userPageC/pages/confirmOrder/confirmOrder?grouponId='+this.data.grouponId,
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isPull == true) {
      var pageNum = this.data.pageNum;
      pageNum++;
      console.log("触底了", pageNum);
      this.setData({
        pageNum: pageNum
      });
      this.getBulkList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})